package com.example.pt.repository;

import com.example.pt.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
    // 根据用户名查找用户是否存在
    boolean existsByUsername(String username);

    // 根据邀请码查找用户
    @Query("SELECT u FROM User u WHERE u.invite_code = :code")
    Optional<User> findByInviteCode(@Param("code") String invite_code);

    // 更新邀请码为null
    @Modifying
    @Query("UPDATE User u SET u.invite_code = null WHERE u.user_id = :user_id")
    void clearInviteCode(@Param("user_id") Long user_id);

    Optional<User> findByPasskey(String passkey);

    Optional<User> findByUsername(String username);

}